

import {createApp} from 'vue'
import App from './App.vue'
import routes from './router'
import store from './store'
import Antd from 'ant-design-vue';
import '@/style/default.less'
import {createRouter, createWebHashHistory} from "vue-router";
import './public-path'
let instance = null;

function render(props = {}) {
    const {container} = props;
    const rootDomName = process.env.VUE_APP_NAME+"Root"
    let app = createApp(App);
    const routerBase = window.__POWERED_BY_QIANKUN__ ? `/#/subject/${process.env.VUE_APP_NAME}` : '/'
    const router = createRouter({
        history: createWebHashHistory(routerBase),
        routes,
    });
    app.config.globalProperties.$getGlobalState = props?.getGlobalState;
    app.config.globalProperties.$onGlobalStateChange = props?.onGlobalStateChange;
    app.config.globalProperties.$offGlobalStateChange = props?.offGlobalStateChange;

    instance = app.use(store)
        .use(router)
        .use(Antd)
        .mount(container ? container.querySelector(`#${rootDomName}`) : `#${rootDomName}`)
    document.getElementById(rootDomName).style.height = container ? '100%': '100vh';
}
if (!window.__POWERED_BY_QIANKUN__) {
    render();
}

export async function bootstrap() {
    // console.log('[vue] vue app bootstraped');
}

export async function mount(props) {
    // console.log('[vue] props from main framework', props);
    render(props);
}

export async function unmount() {
    instance.$unmounted;
    instance.$el.innerHTML = '';
    instance = null;
    // console.log(instance)
}
